package com.myJava.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myJava.entity.PageBean;
import com.myJava.entity.R;
import com.myJava.entity.WxUserInfo;
import com.myJava.service.WxUserInfoService;
import com.myJava.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信用户Controller控制器
 * @author csy
 * 
 * 
 */
@RestController
@RequestMapping("/business/wxUser")
public class WxUserInfoController {

    @Autowired
    private WxUserInfoService wxUserInfoService;

    /**
     * 根据条件分页查询微信用户信息
     * @param pageBean
     * @return
     */
    @PostMapping("/list")
    @PreAuthorize("hasAuthority('business:wxUser:list')")
    public R list(@RequestBody PageBean pageBean){
        String query=pageBean.getQuery().trim();
        Page<WxUserInfo> pageResult = wxUserInfoService.page(new Page<>(pageBean.getPageNum(), pageBean.getPageSize()), new QueryWrapper<WxUserInfo>().like(StringUtil.isNotEmpty(query), "nick_name", query).or().like(StringUtil.isNotEmpty(query), "openid", query));
        List<WxUserInfo> wxUserInfoList = pageResult.getRecords();
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("wxUserInfoList",wxUserInfoList);
        resultMap.put("total",pageResult.getTotal());
        return R.ok(resultMap);
    }


    /**
     * 更新status状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateStatus/{id}/status/{status}")
    @PreAuthorize("hasAuthority('business:wxUser:edit')")
    @Transactional
    public R updateStatus(@PathVariable(value = "id")Integer id,@PathVariable(value = "status")String status){
        WxUserInfo wxUserInfo= wxUserInfoService.getById(id);
        wxUserInfo.setStatus(status);
        wxUserInfoService.saveOrUpdate(wxUserInfo);
        return R.ok();
    }

}
